package com.anis.project.medcare.search.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TableTreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableTree;
import org.eclipse.swt.custom.TableTreeItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;

import com.anis.jpa.medical.model.MedicalReport;
import com.anis.jpa.medical.model.Patient;
import com.anis.project.medcare.Activator;
import com.anis.project.medcare.editors.ReportEditor;
import com.anis.project.medcare.editors.input.ReportEditorInput;
import com.anis.project.medcare.search.tree.providers.SearchReportContentProvider;
import com.anis.project.medcare.search.tree.providers.SearchReportLabelProvider;

public class SearchReportDialog extends Dialog {
	private Text patientNameText;
	private Text patientPhooneText;
	private Text patientLstNameText;
	protected boolean retrieveAll;
	private MedicalReport selectedReport;
	private Text text;
	private Text patientIDText;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public SearchReportDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(4, false));

		final Button btnRetrieveAll = new Button(container, SWT.CHECK);
		btnRetrieveAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (btnRetrieveAll.getSelection()) {
					patientNameText.setEnabled(false);
					patientLstNameText.setEnabled(false);
					patientPhooneText.setEnabled(false);
					patientIDText.setEnabled(false);
					retrieveAll = true;
				} else {
					patientNameText.setEnabled(true);
					patientLstNameText.setEnabled(true);
					patientPhooneText.setEnabled(true);
					patientIDText.setEnabled(true);
					retrieveAll = false;
				}
			}
		});
		btnRetrieveAll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));
		btnRetrieveAll.setText("Retrieve All");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Label lblPatientName = new Label(container, SWT.NONE);
		lblPatientName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
				false, 1, 1));
		lblPatientName.setText("Patient Name :");

		patientNameText = new Text(container, SWT.BORDER);
		patientNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 3, 1));

		Label lblPatientLastName = new Label(container, SWT.NONE);
		lblPatientLastName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				false, false, 1, 1));
		lblPatientLastName.setText("Patient Last Name :");

		patientLstNameText = new Text(container, SWT.BORDER);
		patientLstNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 3, 1));

		Label lblPhone = new Label(container, SWT.NONE);
		lblPhone.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false,
				1, 1));
		lblPhone.setText("Phone :");

		patientPhooneText = new Text(container, SWT.BORDER);
		GridData gd_patientPhooneText = new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1);
		gd_patientPhooneText.widthHint = 125;
		patientPhooneText.setLayoutData(gd_patientPhooneText);

		Label lblPatientId = new Label(container, SWT.NONE);
		lblPatientId.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false,
				false, 1, 1));
		lblPatientId.setText("Patient ID :");

		patientIDText = new Text(container, SWT.BORDER);
		patientIDText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Button btnNewButton = new Button(container, SWT.NONE);

		btnNewButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		btnNewButton.setText("Look up");

		final TableTreeViewer treeViewer = new TableTreeViewer(container,
				SWT.BORDER);
		final TableTree tableTree = treeViewer.getTableTree();
		tableTree.getTable().addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableTreeItem treeItem = tableTree.getSelection()[0];
				Object selectedObject = treeItem.getData();
				if (selectedObject instanceof MedicalReport)
					selectedReport = (MedicalReport) selectedObject;
			}
		});
		tableTree.getTable().setHeaderVisible(true);
		tableTree.getTable().setLinesVisible(true);

		TableColumn patientColumn = new TableColumn(tableTree.getTable(),
				SWT.NONE);
		patientColumn.setWidth(195);
		patientColumn.setText("Patient Name");

		TableColumn reportNameColumn = new TableColumn(tableTree.getTable(),
				SWT.NONE);
		reportNameColumn.setWidth(151);
		reportNameColumn.setText("Report Name");

		TableColumn reportTypeColumn = new TableColumn(tableTree.getTable(),
				SWT.NONE);
		reportTypeColumn.setWidth(178);
		reportTypeColumn.setText("Report Type");
		tableTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5,
				1));

		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				retrieveInputOfTable(retrieveAll, treeViewer);
			}
		});

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(546, 360);
	}

	private void retrieveInputOfTable(boolean retrieveAll,
			TableTreeViewer treeViewer) {
		List<Patient> patientList;
		if (!retrieveAll) {
			String patientName = patientNameText.getText();
			String patientLastName = patientLstNameText.getText();
			Integer patientPhone = null;
			Integer patientID = null;
			if (!patientPhooneText.getText().isEmpty())
				patientPhone = Integer.valueOf(patientPhooneText.getText());
			if (!patientIDText.getText().isEmpty())
				patientID = Integer.valueOf(patientIDText.getText());
			if (patientNameText.getText().isEmpty())
				patientName = null;
			if (patientLstNameText.getText().isEmpty())
				patientLastName = null;

			patientList = Activator
					.getDefault()
					.databaseBinder()
					.retrievePatient(patientName, patientLastName,
							patientPhone, patientID);

		} else {
			patientList = Activator.getDefault().databaseBinder()
					.retrieveAllPatient();
		}
		treeViewer.setLabelProvider(new SearchReportLabelProvider());
		treeViewer.setContentProvider(new SearchReportContentProvider());
		treeViewer.setInput(patientList);
	}

	@Override
	protected void okPressed() {
		super.okPressed();
		ReportEditorInput preportEditorInput = new ReportEditorInput(
				selectedReport);
		try {
			ReportEditor reportEditor = (ReportEditor) Activator.getDefault()
					.getWorkbench().getActiveWorkbenchWindow().getActivePage()
					.openEditor(preportEditorInput, ReportEditor.ID);
		} catch (PartInitException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}
}
